%% Time-Frequency Domian Robust Interference Mitigation(TFDRIM)
%% GPS L1 Receiver
% Author  : Xiaoyeyimier
% Date    : 2021-12-22
% Version : 1.0.1
clear all;
DevSettings.HistoryDataPath = uigetdir('*.*','请选择文件夹');
files = dir(DevSettings.HistoryDataPath);
m = length(files);
for cnt = 1:m
    cmpvarI = ~isempty(regexp(files(cnt).name,'IData','match'));
    cmpvarQ = ~isempty(regexp(files(cnt).name,'QData','match'));
    if cmpvarI
        DevSettings.HistoryDataNameI = files(cnt).name;
    end
    if cmpvarQ
        DevSettings.HistoryDataNameQ = files(cnt).name;
    end
end
check = isfield(DevSettings,'HistoryDataNameI')&&...
    isfield(DevSettings,'HistoryDataNameQ');
if ~check
    msgbox('当前文件夹下没有适合的数据进行回放');
end
[DevSettings,check] = readLog(DevSettings);

%% STFT settings ==========================================
STFT_SETTINGS.window_len     = 512;
STFT_SETTINGS.fft_length     = 1024;
STFT_SETTINGS.fft_len        = 1024;
STFT_SETTINGS.STEP_T         = 50;   
STFT_SETTINGS.gaussian_param = 10;  
STFT_SETTINGS.window_type    = 1;    
STFT_SETTINGS.Fs             = DevSettings.Fs;
STFT_SETTINGS.current_folder = DevSettings.HistoryDataPath;

[flag] = mitigation(DevSettings,STFT_SETTINGS)
%% Read Data 
%-------------------------------------------------------------------------%
Times2Read = 1000;                                                          % Times to read [ms] (for debug and acq)
Samples2Read = DevSettings.Fs*Times2Read*1e-3;                             % cal samples 2 read
if ~DevSettings.RealTimeProcess                                            % read a realtime data
    FidI = fopen([DevSettings.HistoryDataPath,...
                  DevSettings.HistoryDataName,'Idata.',...
                  DevSettings.FileType],'r');
    FidQ = fopen([DevSettings.HistoryDataPath,...
                  DevSettings.HistoryDataName,'Qdata.',...
                  DevSettings.FileType],'r');
else                                                                       % read a history data
    FidI = fopen(NameI,'r');
    FidQ = fopen(NameQ,'r');
end
fseek(FidI,Samples2Read*8*0.5,'bof');                                      % skip some unstable data from the beginning
fseek(FidQ,Samples2Read*8*0.5,'bof');                                      % skip some unstable data from the beginning
dataI = fread(FidI,[1 Samples2Read],DevSettings.dataType);                 % read I data
dataQ = fread(FidQ,[1 Samples2Read],DevSettings.dataType);                 % read Q data
longSignal = dataI+1j*dataQ;                                               % Combine IQ to a complex signal
